package com.turnengine.client.local.upkeep.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.local.upkeep.bean.IUpkeep;
import com.turnengine.client.local.upkeep.bean.IUpkeepCondition;
import com.turnengine.client.local.upkeep.command.AddUpkeep;
import com.turnengine.client.local.upkeep.command.AddUpkeepCondition;
import com.turnengine.client.local.upkeep.command.GetUpkeepConditionList;
import com.turnengine.client.local.upkeep.command.GetUpkeepList;
import com.turnengine.client.local.upkeep.command.IAddUpkeep;
import com.turnengine.client.local.upkeep.command.IAddUpkeepCondition;
import com.turnengine.client.local.upkeep.command.IGetUpkeepConditionList;
import com.turnengine.client.local.upkeep.command.IGetUpkeepList;
import com.turnengine.client.local.upkeep.enums.UpkeepConditionType;
import java.util.List;

/**
 * The Local Upkeep Command Set.
 */
public class LocalUpkeepCommandSet implements ILocalUpkeepCommandSet {

	@Override
	public IUpkeep addUpkeep(long loginId, int instanceId, int upkeepId, int priority) {
		IAddUpkeep command = new AddUpkeep(loginId, instanceId, upkeepId, priority);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUpkeep> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IUpkeepCondition addUpkeepCondition(long loginId, int instanceId, int upkeepId, UpkeepConditionType type, int conditionId, long conditionAmount) {
		IAddUpkeepCondition command = new AddUpkeepCondition(loginId, instanceId, upkeepId, type, conditionId, conditionAmount);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUpkeepCondition> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IUpkeep> getUpkeepList(long loginId, int instanceId) {
		IGetUpkeepList command = new GetUpkeepList(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IUpkeep>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IUpkeepCondition> getUpkeepConditionList(long loginId, int instanceId) {
		IGetUpkeepConditionList command = new GetUpkeepConditionList(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IUpkeepCondition>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
